/****************************************************
 * QR-Code Bookmarklet
 *  
 * Author: Tyler Larson <devel@tlarson.com>
 * License: GPL v2
 * 
/****************************************************/

// Bookmarklet Syntax
//   javascript:(function(){s=document.createElement('script');s.type='text/javascript';s.src='http://quickqr.googlecode.com/svn/trunk/quickqr.js';document.body.appendChild(s);})();

// ****************
// jQuery Loader
(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="http://ajax.googleapis.com/ajax/libs/jquery/"+g+"/jquery.min.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"1.3.2",function($,L){

	var chart_url="http://chart.apis.google.com/chart?cht=qr&chs=300x300&chld=H|0&chl=";
	
	var html='<div id="__qr__" style="color:black;font-family:arial;font-size:14px;border:0;margin:0;line-height:1.3em"><div style="position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,.6);z-index:99"><div style="width:100%;position:absolute;top:100px"><div style="width:600px;height:450px;background:rgba(255,255,255,0.9);margin:0 auto auto;-webkit-border-radius: 20px;-moz-border-radius: 20px;border-radius: 20px;-moz-box-shadow: 2px 2px 7px rgba(0,0,0,0.7);-webkit-box-shadow: 2px 2px 7px rgba(0,0,0,0.7);box-shadow: 2px 2px 7px rgba(0,0,0,0.7);padding:15px"><div style="float:right;padding-right:35px"><a href="javascript:void()" id="__qr_close" style="color:black;text-decoration:none">[ Close ]</a></div><div style="border:1px solid #999;background:white;padding:3px;height:350px;width:500px;margin:auto;margin-top:25px;padding:25px;-webkit-border-radius:10px;-moz-border-radius:10px;border-radius:10px"><div style="overflow:hidden"><img id="__qr_img" src="" height="300" width="300" alt="QR Code" style="float:left"><div style="margin-left:310px;margin-top:5px"><div style="margin-top:10px"><button id="__qr_gen_short">Create shortened link</button></div><div style="border:1px solid #99F;background:#CCF;padding:2px;display:none" id="__qr_short">Shortened Link<br><div style="border:1px solid #99F;padding:1px;background:white"><a style="color:#99C" id="__qr_shortlink"></a></div></div><div style="margin-top:10px"><button style="display:none" id="__qr_reset">Reset URL</button></div><div style="margin-top:10px;color:red;display:none" id="__qr_err"></div></div></div><div style="margin-top:10px">Encoded URL:<br><button style="float:right;margin-right:5px" id="__qr_set">Set</button><input id="__qr_urlbar" style="width:440px"></div></div></div></div></div></div>';
	
	var orig_url = document.location.toString();

	// *****************
	// Clean-up script
	function cleanup() {
		$("#__qr__").remove();
	}
	function quoteurl(str) {
		return str.replace(/&/g,"%26");
	}
	function setimage(url,silent) {
		$("#__qr_img").attr('src',chart_url+quoteurl(url));
		if (!silent) { $("#__qr_urlbar").val(url); }
	}

	function init() {
		$(html).appendTo(document.body);
		setimage(orig_url);
		$("#__qr_close").click(cleanup);
		$("#__qr_set").click(set);
		$("#__qr_reset").click(reset);
		$("#__qr_gen_short").click(makeshort);
		$("#__qr_urlbar").keypress(function(e) {
			if(e.which == 13) {
				set();
			}
		});
	}

	function set() {
		setimage($("#__qr_urlbar").val());
		$("#__qr_reset").show();
	}

	function reset() {
		setimage(orig_url);
		$("#__qr_reset").hide();
	}
	
	function makeshort() {
		$("#__qr_err").hide();
		getshort($("#__qr_urlbar").val());
		$("#__qr_gen_short").hide();
	}
	
	function getshort(url) {
		var opts = { 
			version: '2.0.1',
			login: 'anon111', // your bit.ly account login
			apiKey: 'R_b247eaf36c2bcad1791e9beb9a123bb5', // your api key here
			history: '0' };
		var daurl = "http://api.bit.ly/shorten?"
			+"version="+opts.version
			+"&longUrl="+quoteurl(url)
			+"&login="+opts.login
			+"&apiKey="+opts.apiKey
			+"&history="+opts.history
			+"&format=json&callback=?";
		$.getJSON(daurl, function(data){
			try {
			var surl = data.results[url].shortUrl;
			} catch (Exception) {
				$("#__qr_gen_short").show();
				$("#__qr_err").text("Error").show();
				$("#__qr_err").text("Error: " + data.errorMessage);
			}
			$("#__qr_shortlink").attr("href",surl).text(surl);
			$("#__qr_short").show();
			setimage(surl,true);
			
		});
	}

	init();
});